package com.example.serverSide.commom.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.example.serverSide.commom.util.BaseContext;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createDate", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject,"creator", () -> BaseContext.getCurrentId(), Long.class);
        this.strictUpdateFill(metaObject, "updateDate", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictUpdateFill(metaObject,"updater", () ->BaseContext.getCurrentId(), Long.class);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateDate", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictUpdateFill(metaObject,"updater", () ->BaseContext.getCurrentId(), Long.class);
    }
}
